package com.example.book.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.FastDateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDD = "yyyyMMdd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH", "yyyy.MM",
            "yyyy年MM月dd日", "yyyy年MM月dd日 HH时mm分ss秒", "yyyy年MM月dd日 HH时mm分", "yyyy年MM月dd日 HH时", "yyyy年MM月",
            "yyyy"};

    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf = new SimpleDateFormat("MMM-d-yyyy K:m:s a", Locale.ENGLISH);
    private static Calendar calendar = Calendar.getInstance();

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static String formatDate(Date date){
        return format.format(date);
    }

    public static String formatDate() {
        Date date = new Date();
        return formatDate(date, "yyyyMMdd");
    }

    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, String pattern) {
        String formatDate = null;
        if (date != null) {
//			if (StringUtils.isNotBlank(pattern)) {
//				formatDate = DateFormatUtils.format(date, pattern);
//			} else {
//				formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
//			}
            if (StringUtils.isBlank(pattern)) {
                pattern = "yyyy-MM-dd";
            }
            formatDate = FastDateFormat.getInstance(pattern).format(date);
        }
        return formatDate;
    }

    public static String getBeforeTime(String date) throws ParseException {

        String val = null;
        String a = date.substring(12, 14);
        if (a.equals("12")) {
            String f = date.substring(0, 11);
            String l = date.substring(14, 23);
            val = f + " 00" + l;
        } else {
            val = date;
        }

        Date d2 = sdf.parse(val);
        Date d = format.parse(format.format(d2));
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.HOUR, +8);
        return format.format(cal.getTime());
    }

    /**
     * 当前月的第几天
     *
     * @return
     */
    public static String getDayOfMonth() {
        calendar.setTime(new Date());
        int i = calendar.get(Calendar.DAY_OF_MONTH);
        return String.valueOf(i);
    }

    /**
     * 根据传入时间获取当前月第几天
     *
     * @param date
     * @return
     */
    public static int getDayOfMonth(Date date) {
        calendar.setTime(date);
        int i = calendar.get(Calendar.DAY_OF_MONTH);
        return i;
    }

    public static String getMonth() {
        calendar.setTime(new Date());
        int i = calendar.get(Calendar.MONTH) + 1;
        return String.valueOf(i);
    }

    public static String getMonth(Date date) {
        calendar.setTime(date);
        int i = calendar.get(Calendar.MONTH) + 1;
        return String.valueOf(i);
    }

    public static String getYear() {
        calendar.setTime(new Date());
        int i = calendar.get(Calendar.YEAR);
        return String.valueOf(i);
    }

    public static String getYear(Date date) {
        calendar.setTime(date);
        int i = calendar.get(Calendar.YEAR);
        return String.valueOf(i);
    }

    /**
     * 获取上一月第一天
     *
     * @return
     */
    public static Date getLastMothFirstDay() {
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取上一月 最后一天
     *
     * @return
     */
    public static Date getLastMothLastDay() {
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, 0);
        return calendar.getTime();
    }

    /**
     * 获取月份有多少天
     *
     * @return
     */
    public static int getDayNumber() {
        calendar.setTime(new Date());
        int i = calendar.getActualMaximum(Calendar.DATE);
        return i;
    }


    /**
     * 获取几天前的时间
     *
     * @param day
     * @return
     */
    public static Date getDateBefore(int day) {
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
        return calendar.getTime();
    }


    public static Date getDateBefore(Date date, int day) {
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
        return calendar.getTime();
    }

    /**
     * 获取指定的月份有多少天
     *
     * @param date
     * @return
     */
    public static int getDayNumber(Date date) {
        calendar.setTime(date);
        int i = calendar.getActualMaximum(Calendar.DATE);
        return i;
    }

}
